﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ChinaGS.Lian.ModuleRemoting;

public partial class manage_user_paid_record : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1_DataBind();
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取要删除的记录的id并赋值给变量ID        
            int ID = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);
            String sql = "delete from t_user_paid where user_paid_id=" + ID.ToString();
            DataBaseOperate_SQLServer.ExecUpdate(sql);
            GridView1_DataBind();   
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
        if(e.CommandName == "Backout"){
            int ID = Convert.ToInt32(e.CommandArgument);
            String sql1 = "select item_no, serial_no, operate_status from t_user_paid where user_paid_id=" + ID.ToString();
            DataRow dr = DataBaseOperate_SQLServer.GetDataRow(sql1);
            if (dr["item_no"] == null || dr["serial_no"] == null)
            {
                LabMessage.Text = "缺少值，不能执行冲正操作";
            }
            else if (dr["operate_status"].ToString() == "4")
            {
                LabMessage.Text = "不能对同冲正成功记录，重复冲正";
            }
            else
            {
                //冲正前操作
                String sqlSetstatus1 = "exec p_paid_setstatus @user_paid_id =" + ID.ToString() + ", @serial_no='" + dr["serial_no"] + "', @item_no='" + dr["item_no"] + "', @remark='beforbackout', @operate_status=3, @user_ip='" + SysCommon.getClientIP() + "', @admin_id=1 ";
                DataTable result1 = DataBaseOperate_SQLServer.GetDataTable(sqlSetstatus1);
                LabMessage.Text = "冲正中" + result1.Rows[0][0].ToString();
                MobilePackage mp = RemotingClass.mobileBackOut(dr["item_no"].ToString(), dr["serial_no"].ToString());                
                if (mp.Status)
                {
                    //冲正成功操作
                    String sqlSetstatus2 = "exec p_paid_setstatus @user_paid_id =" + ID.ToString() + ", @serial_no='" + dr["serial_no"] + "', @item_no='" + dr["item_no"] + "', @remark='" + mp.Message + "', @operate_status=4, @user_ip='" + SysCommon.getClientIP() + "', @admin_id=1 ";
                    DataTable result = DataBaseOperate_SQLServer.GetDataTable(sqlSetstatus2);
                    LabMessage.Text = "冲正成功"+result.Rows[0][0].ToString();
                    GridView1_DataBind();   
                }
                else
                {
                    String sqlSetstatus3 = "exec p_paid_setstatus @user_paid_id =" + ID.ToString() + ", @serial_no='" + dr["serial_no"] + "', @item_no='" + dr["item_no"] + "', @remark='" + mp.Message + "', @operate_status=5, @user_ip='" + SysCommon.getClientIP() + "', @admin_id=1 ";
                    DataTable result = DataBaseOperate_SQLServer.GetDataTable(sqlSetstatus3);
                    LabMessage.Text = "冲正失败" + result.Rows[0][0].ToString();
                    GridView1_DataBind();   
                }
            }
        }           
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        //进行分页之后，重新部署数据
        GridView1_DataBind();   
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //分页完成之前
        GridView1.PageIndex = e.NewPageIndex;
    }


    private void GridView1_DataBind()
    {
        String sql = "select user_paid_id, item_no, serial_no, case operate_status when 0 then '支付前' when 1 then '支付成功' when 2 then '支付失败' when 3 then '冲正前' when 4 then '已冲正' when 5 then '冲正失败' end as operate_status, pay_time from t_user_paid";
        GridView1.DataSource = DataBaseOperate_SQLServer.GetDataTable(sql);
        GridView1.DataBind();
    }

}
